# -*- coding: utf-8 -*-
from django.core.mail import send_mail,EmailMessage,SMTPConnection
from django.http import HttpResponse
from utils import formatter,function,uploader,logs
from utils.helper import view
import threading,time,traceback
from settings import *

def send(subject,body,recipient_list):
    """
    summary:
        发送邮件功能封装
    params:
        subject    标题
        body        正文,可以是HTML的
        recipient_list    收件人列表如：["test@coderblogs.com","test2@coderblogs.com"]
    author:
        李华顺 <huacnlee@gmail.com>
    """
    try:
        connection = SMTPConnection(
                                    username=EMAIL_HOST_USER,
                                    password=EMAIL_HOST_PASSWORD,
                                    fail_silently=False
                                    )
        msg = EmailMessage(
                         EMAIL_SUBJECT_PREFIX + subject,
                         body,
                         EMAIL_HOST_USER,
                         recipient_list,
                         connection=connection
                         )

        msg.content_subtype = "html"
        msg.send()
    except:
        logs.write(traceback)


class SignupActive(threading.Thread):
    """
    summary:
        注冊申请验证通过提示
    author:
        李华顺 <huacnlee@gmail.com>
    """
    user = None

    def __init__(self,user):
        threading.Thread.__init__(self)
        self.user = user


    def run(self):
        data = {
                "user": self.user,
                }
        try:
            body = view.out_html("email/signup_active.mail",data)
            send(
                 "注冊申请通过",
                 body,
                 [self.user.email])
            logs.write("Email发送成功！")
        except:
            logs.write(traceback)
        time.sleep(1)

class GotComment(threading.Thread):
    """
    summary:
        收到评论
    author:
        李华顺 <huacnlee@gmail.com>
    """
    comment = None
    
    def __init__(self,comment):
        threading.Thread.__init__(self)
        self.comment = comment
    
    def run(self):
        data = {
                "comment" : self.comment,
                }
        
        try:
            body = view.out_html("email/got_comment.mail",data)
            send('文章收到新评论',body,[self.comment.post.profile.user.email])
        except:
            logs.write(traceback)            
        time.sleep(1)

class FollowComment(threading.Thread):
    """
    summary:
        收到评论
    author:
        李华顺 <huacnlee@gmail.com>
    """
    comment = None
    
    def __init__(self,comment):
        threading.Thread.__init__(self)
        self.comment = comment
    
    def run(self):
        data = {
                "comment" : self.comment,
                }
        
        try:
            body = view.out_html("email/follow_comment.mail",data)
            send('您关注文章有新评论',body,[self.comment.email])
        except:
            logs.write(traceback)            
        time.sleep(1)
       
        
        